﻿package com.xeem.henkel.common
{	
	import fl.motion.MatrixTransformer;
	
	import flash.display.*;
	import flash.events.*;
	import flash.geom.*;
	import flash.text.*;

	
	public class CircleComponent extends UIBaseComponent
	{

		private var _btn:MovieClip;
		private var _txtValue:TextField;
		private var old_A:Number=0;
		private var _languageUnit:LanguageModel;
		private var _type:int=1;
		private var m:Matrix;
		private var _minUnit:Number=0;
		private var tempX:Number;
		private var tempY:Number;
		private var _rotationX:Number;
		private var	_rotationY:Number;
		
		public function CircleComponent(contain:DisplayObjectContainer,type:int=1,minUnit:Number=0)
		{
			if(contain!=null)   
				contain.addChild(this); 
			var bg : MovieClip;
			if (type==1)
				bg= new bgCircle1();				
			if (type==2)
				bg= new bgCircle2();			
			this._type=type;
			this._minUnit=minUnit;
			addChild(bg);			
		}
		
		public function draw():void{
			drawData();
			drawLanguage();
		}
		
		public override function drawData():void
		{
			_languageUnit=new LanguageModel();
			_languageUnit.fontSize="10";
			
			_languageUnit.text=div(data.minData,data.precision)+data.unit;	
			_txtValue=_languageUnit.getTextField();	
			_btn=new bar();
			_btn.buttonMode=true;
			
			if (_type==1){
				_btn.x=30;
				_btn.y=20;
				_rotationX=19;
				_rotationY=20.8;
				_txtValue.x=13;
				_txtValue.y=32;		
				old_A=118;
			}
			
			if (_type==2){				
				_btn.x=30;
				_btn.y=18;
				_txtValue.x=13;
				_txtValue.y=30;	
				_rotationX=19;
				_rotationY=18.5;
				old_A=120;
			}
		
			m = _btn.transform.matrix;
			MatrixTransformer.rotateAroundExternalPoint(m,_rotationX,_rotationY,old_A);
			_btn.transform.matrix=m;
			
			tempX=_btn.x;
			tempY=_btn.y;
			addChild(_btn);
			addChild(_txtValue);			
			this.value=data.minData;
			_btn.addEventListener(MouseEvent.MOUSE_DOWN, onStartDragHandler);
			_btn.stage.addEventListener(MouseEvent.MOUSE_UP,onStopDragHandler);   
			//_btn.addEventListener(MouseEvent.MOUSE_OUT,onStopDragHandler);   
		}   
		
		private function onStartDragHandler(event:MouseEvent):void  
		{   
			addEventListener(MouseEvent.MOUSE_MOVE,onMoveHandler);
		}   
		
		
		private function onMoveHandler(e:MouseEvent):void  
		{  
			var new_A:Number = Math.atan2(mouseY-_rotationY,mouseX-_rotationX) *180/ Math.PI;//这里需要角度的大小比较
			if (_type==1 && new_A>60 &&  new_A<130) return;
			if (_type==2 && new_A>70 &&  new_A<120) return;			
			if (new_A==old_A) return;
			m = _btn.transform.matrix.clone();
			MatrixTransformer.rotateAroundExternalPoint(m,_rotationX,_rotationY,new_A-old_A);
			//重新设置值 防止数据设置时有偏差
			m.a=Math.round(m.a*Math.pow(10,7))/Math.pow(10,7);
			m.b=Math.round(m.b*Math.pow(10,7))/Math.pow(10,7);
			m.c=Math.round(m.c*Math.pow(10,7))/Math.pow(10,7);
			m.d=Math.round(m.d*Math.pow(10,7))/Math.pow(10,7);
			m.tx=Math.round(m.tx*Math.pow(10,1))/Math.pow(10,1);
			m.ty=Math.round(m.ty*Math.pow(10,1))/Math.pow(10,1);
			//trace(m.toString());
			_btn.transform.matrix = m;
			//trace(_btn.transform.matrix.toString());
			old_A=new_A;
			//_btn.x=1/Math.tan(new_A)*14+_rotationX;
			if (_type==1){
				if (new_A>=130){
					new_A-=130;
				}else{
					new_A+=230;
				}	
				
				if (_minUnit==0)
					this.value=new_A/280*data.rangeData+data.minData;
				else
					this.value=((int)(new_A/(280/(data.rangeData/_minUnit))))*_minUnit+data.minData;
				if (data.precision==0){
					this.value=Math.round(this.value);
				}else{
					this.value=Math.round(this.value*Math.pow(10,data.precision))/Math.pow(10,data.precision);
					_txtValue.x=13;
				}
				_txtValue.text=div(this.value,data.precision)+" "+data.unit;	
				_txtValue.setTextFormat(_languageUnit.getTextFormat());
			}
			
			if (_type==2){
				if (new_A>=120){
					new_A-=120;
				}else{
					new_A+=240;
				}
				if (_minUnit==0)
					this.value=new_A/300*data.rangeData+data.minData;
				else
					this.value=((int)(new_A/(300/(data.rangeData/_minUnit))))*_minUnit+data.minData;
				//trace(((int)(new_A/(300/(data.rangeData/_minUnit)))));
				if (data.precision==0){
					this.value=Math.round(this.value);
				}else{
					this.value=Math.round(this.value*Math.pow(10,data.precision))/Math.pow(10,data.precision);
					_txtValue.x=13;
				}
				_txtValue.text=div(this.value,data.precision)+" "+data.unit;	
				_txtValue.setTextFormat(_languageUnit.getTextFormat());
			}			
			reDraw();	
		}   
		
		private function onStopDragHandler(event:MouseEvent):void  
		{   
			removeEventListener(MouseEvent.MOUSE_MOVE,onMoveHandler); 
		}   
		
		public override function reDraw():void
		{
			var evt:Event=new Event(Event.CHANGE);   
			this.dispatchEvent(evt); 
		}
		
		private function round(x:Number,p:int):Number{
			if (p==0){
				return Math.round(x);
			}else{
				return Math.round(x*Math.pow(10,p))/Math.pow(10,p);
			}
		}
		
		private function div(x:Number,p:int):String{
			if (p==0) return x.toString();
			var t:String="";
			var ii:int=0;
			for (;ii<=p;ii++){
				if (x*Math.pow(10,ii)==int(x*Math.pow(10,ii))) break;
			} 
			trace("ii=>"+ii.toString());
			if (ii<p){
				if (ii==0)t+=".";
				while(ii<p){
					t+="0";
					ii++;
				}
				return x.toString()+t;
			}else{
				return x.toString();
			}			
		}
	}
}